package com.example.dryyzx.dao;

import com.example.dryyzx.encapsulation.NursingRecordEncapsulation;
import com.example.dryyzx.entity.NursingRecord;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

import java.util.ArrayList;

public interface NursingRecordMapper {
    @Delete({
        "delete from nursing_record",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into nursing_record (id, customer_id, ",
        "nurse_id, nursing_content_id, ",
        "start_time, end_time, ",
        "enable)",
        "values (#{id,jdbcType=INTEGER}, #{customerId,jdbcType=INTEGER}, ",
        "#{nurseId,jdbcType=INTEGER}, #{nursingContentId,jdbcType=INTEGER}, ",
        "#{startTime,jdbcType=TIMESTAMP}, #{endTime,jdbcType=TIMESTAMP}, ",
        "#{enable,jdbcType=BIT})"
    })
    int insert(NursingRecord record);

    @InsertProvider(type=NursingRecordSqlProvider.class, method="insertSelective")
    int insertSelective(NursingRecord record);

    @Select({
        "select",
        "id, customer_id, nurse_id, nursing_content_id, start_time, end_time, enable",
        "from nursing_record",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="customer_id", property="customerId", jdbcType=JdbcType.INTEGER),
        @Result(column="nurse_id", property="nurseId", jdbcType=JdbcType.INTEGER),
        @Result(column="nursing_content_id", property="nursingContentId", jdbcType=JdbcType.INTEGER),
        @Result(column="start_time", property="startTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="end_time", property="endTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="enable", property="enable", jdbcType=JdbcType.BIT)
    })
    NursingRecord selectByPrimaryKey(Integer id);

    @Select({
            "select",
            "nursing_record.id, customer.name customer_name, customer_id,nursing_content.name project_name, nursing_record.start_time,user.name user_name",
            "from nursing_record,customer,`user`,nursing_content",
            "where nursing_record.customer_id = customer.id and nursing_record.nurse_id = user.id",
            "and nursing_record.nursing_content_id = nursing_content.id",
            "and nursing_record.enable = 1",
            "order by nursing_record.id",
            "limit #{start,jdbcType=INTEGER},#{number,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="customer_name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="customer_id", property="customerId", jdbcType=JdbcType.INTEGER),
            @Result(column="project_name", property="project", jdbcType=JdbcType.VARCHAR),
            @Result(column="start_time", property="date", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="user_name", property="uname", jdbcType=JdbcType.VARCHAR)
    })
    ArrayList<NursingRecordEncapsulation> selectByPage(int start, int number);

    @Select({
            "select",
            "count(*)",
            "from nursing_record",
            "where enable = 1"

    })
    int countRecordNumber();

    @UpdateProvider(type=NursingRecordSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(NursingRecord record);

    @Update({
        "update nursing_record",
        "set customer_id = #{customerId,jdbcType=INTEGER},",
          "nurse_id = #{nurseId,jdbcType=INTEGER},",
          "nursing_content_id = #{nursingContentId,jdbcType=INTEGER},",
          "start_time = #{startTime,jdbcType=TIMESTAMP},",
          "end_time = #{endTime,jdbcType=TIMESTAMP},",
          "enable = #{enable,jdbcType=BIT}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(NursingRecord record);
}